const getLocalIP = require('@/utils/getLocalIP');
const port = process.env.PORT || 3000;
const localIP = getLocalIP();
/**
 * @param {Array} servers 服务器地址
 */
const servers = [
  {
    url: `http://localhost:${port}/api`, // API服务器地址
    description: '本地环境',
  },
  {
    url: `http://${localIP}:${port}/api`, // API服务器地址
    description: 'IP地址',
  },
]


/**
 * @param {Object} components 组件
*/
const components = {
  schemas: {
    // 请求成功
    success: {
      type: 'object',
      properties: {
        status: { type: 'string', example: 'success', description: '请求状态' },
        code: { type: 'string', example: '200', description: '状态码' },
        message: { type: 'string', example: '请求成功', description: '返回信息' }
      }
    },
    // 请求参数错误
    paramsBad: {
      type: 'object',
      properties: {
        status: { type: 'string', example: 'error', description: '请求状态' },
        code: { type: 'string', example: '400', description: '状态码' },
        message: { type: 'string', example: '参数错误', description: '返回信息' }
      }
    },
    // 未登录
    unauthorized: {
      type: 'object',
      properties: {
        status: { type: 'string', example: 'error', description: '请求状态' },
        code: { type: 'string', example: '401', description: '状态码' },
        message: { type: 'string', example: '未登录', description: '返回信息' }
      }
    },
    // 请求失败
    error: {
      type: 'object',
      properties: {
        status: { type: 'string', example: 'error', description: '请求状态' },
        code: { type: 'string', example: '500', description: '状态码' },
        message: { type: 'string', example: '请求失败', description: '返回信息' }
      }
    },
    // 用户信息
    userInfo: {
      type: 'object',
      properties: {
        id: { type: 'string', example: '123456789', description: '用户ID' },
        account: { type: 'string', example: 'admin', description: '用户名' },
        nickname: { type: 'string', example: 'John Doe', description: '用户昵称' },
        roleIds: { type: 'string', example: [1, 2], description: '用户角色ID' },
        phoneNumber: { type: 'string', example: '1234567890', description: '手机号' },
        email: { type: 'string', example: 'admin@example.com', description: '用户邮箱' },
        avatar: { type: 'string', example: 'https://example.com/avatar.jpg', description: '用户头像' },
        createdAt: { type: 'string', example: '2023-01-01T00:00:00.000Z', description: '创建时间' },
        updateAt: { type: 'string', example: '2023-01-01T00:00:00.000Z', description: '更新时间' }
      }
    },
    // 更新用户信息
    updataUser: {
      type: 'object',
      properties: {
        nickname: { type: 'string', example: '管理员', description: '用户昵称' },
        // password: { type: 'string', example: '123456', description: '用户密码' },
        roleIds: { type: 'array', example: [1, 2], description: '角色ID' },
        phoneNumber: { type: 'string', example: '1806610689', description: '手机号' },
        email: { type: 'string', example: '766187397@qq.com', description: '用户邮箱' },
        avatar: { type: 'string', example: '/static/images/1731140422571-7aa83266b8b64fafbfc6e39a281df3e0.jpg', description: '用户头像' },
        frontCryptoState: { type: 'boolean', example: false, description: '是否开启前端加密' }
      }
    }
  },
  responses: {
    // 请求成功
    success: {
      description: '请求成功',
      content: {
        'application/json': {
          schema: {
            $ref: '#/components/schemas/success'
          }
        }
      }
    },
    // 请求参数错误
    paramsBad: {
      description: '请求参数错误',
      content: {
        'application/json': {
          schema: {
            $ref: '#/components/schemas/paramsBad'
          }
        }
      }
    },
    // 未登录
    unauthorized: {
      description: '未登录',
      content: {
        'application/json': {
          schema: {
            $ref: '#/components/schemas/unauthorized'
          }
        }
      }
    },
    // 请求失败
    error: {
      description: '请求失败',
      content: {
        'application/json': {
          schema: {
            $ref: '#/components/schemas/error'
          }
        }
      }
    },
    // 用户信息
    userInfo: {
      description: '用户信息',
      content: {
        'application/json': {
          schema: {
            $ref: '#/components/schemas/userInfo'
          }
        }
      }
    }
  },
  securitySchemes: {
    authorization: {
      type: 'http',
      scheme: 'bearer',
      bearerFormat: 'JWT'
    }
  }
}

module.exports = {
  servers,
  components
}